package com.hdaccp.ch10

import java.sql.{Connection, PreparedStatement}

import com.hdaccp.ch07.Mu1
import com.hdaccp.log.MySQLUtils

import scala.collection.mutable.ListBuffer

object MyLogByDateDao {
  def insertMyLogByDate(list:ListBuffer[MyLogByDateState]): Unit = {

    var connection: Connection = null
    var pstmt: PreparedStatement = null

    try {
      connection = MySQLUtils.getConnection()

      connection.setAutoCommit(false) //设置手动提交

      val sql = "insert into mylogbydate(fangdate,coursecode,times) values (?,?,?) "
      pstmt = connection.prepareStatement(sql)


      for (ele <- list) {
        pstmt.setString(1,ele.fangdate)
        pstmt.setString(2, ele.coursecode)
        pstmt.setLong(3, ele.times)
        pstmt.addBatch()
      }

      pstmt.executeBatch() // 执行批量处理
      connection.commit() //手工提交



    } catch {
      case e: Exception => e.printStackTrace()
    } finally {
      MySQLUtils.release(connection, pstmt)
    }
  }

}
